﻿using FI.Core;
using FI.EntityFramework.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FI.EntityFramework
{
    /// <summary>   
    /// Represents a view to navigate the <see cref="EntityFrameworkSettings"/> configuration data.
    /// </summary>
    public static class EntityFrameworkConfigurationView
    {
        private static readonly EntityFrameworkSettings settings = ConfigManager.GetConfigurationSection<EntityFrameworkSettings>();

        /// <summary>
        /// Gets the field name of version no in database.
        /// </summary>
        /// <value>The version no field.</value>
        public static string VersionNoField
        {
            get { return GetVersionNoField(); }
        }

        /// <summary>
        /// Gets the field name of transaction id in database.
        /// </summary>
        /// <value>The transaction id field.</value>
        public static string TransactionIdField
        {
            get { return GetTransactionIdField(); }
        }

        /// <summary>
        /// Gets the field name of created by in database.
        /// </summary>
        /// <value>The created by field.</value>
        public static string CreatedByField
        {
            get { return GetCreatedByField(); }
        }

        /// <summary>
        /// Gets the field name of created date in database.
        /// </summary>
        /// <value>The created time field.</value>
        public static string CreatedTimeField
        {
            get { return GetCreatedTimeField(); }
        }

        /// <summary>
        /// Gets the field name of last updated by in database.
        /// </summary>
        /// <value>The last updated by field.</value>
        public static string LastUpdatedByField
        {
            get { return GetLastUpdatedByField(); }
        }

        /// <summary>
        /// Gets the field name of last updated date in database.
        /// </summary>
        /// <value>The last updated time field.</value>
        public static string LastUpdatedTimeField
        {
            get { return GetLastUpdatedTimeField(); }
        }

        /// <summary>
        /// Gets the i connect state field.
        /// </summary>
        /// <value>
        /// The i connect state field.
        /// </value>
        public static string DataStateField
        {
            get { return GetIConnectStateField(); }
        }

        /// <summary>
        /// Gets the field name of version no in database.
        /// </summary>
        /// <value>The version no field.</value>
        public static string GetVersionNoField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.VersionNo);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of transaction id in database.
        /// </summary>
        /// <value>The transaction id field.</value>
        public static string GetTransactionIdField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.TransactionId);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of created by in database.
        /// </summary>
        /// <value>The created by field.</value>
        public static string GetCreatedByField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.CreatedBy);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of created date in database.
        /// </summary>
        /// <value>The created time field.</value>
        public static string GetCreatedTimeField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.CreatedTime);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of last updated by in database.
        /// </summary>
        /// <value>The last updated by field.</value>
        public static string GetLastUpdatedByField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.LastUpdatedBy);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of last updated date in database.
        /// </summary>
        /// <value>The last updated time field.</value>
        public static string GetLastUpdatedTimeField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.LastUpdatedTime);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the i connect state field.
        /// </summary>
        /// <param name="entityFrameworkSettings">The entity framework settings.</param>
        /// <returns></returns>
        public static string GetIConnectStateField(EntityFrameworkSettings entityFrameworkSettings = null)
        {
            entityFrameworkSettings = entityFrameworkSettings ?? settings;
            SystemFieldData data = entityFrameworkSettings.SystemFields.Get(SystemFieldData.IConnectState);
            if (data == null)
            {
                return "State";
            }
            return data.FieldName;
        }
    }
}
